local KofTopView = class("KofTopView",function()
    return display.newLayer()
end)

function KofTopView:ctor(isSelfLeft)
    self.isSelfLeft = isSelfLeft
    --ui root
--    self.mUINode = cc.CSLoader:createNode(Res.BattleKofTopNode, false, true)
    self.mUINode = MainBattleUICache:find(Res.BattleKofTopNode)
    self.mUINode:setAnchorPoint(0, 0)
    self.mUINode:setContentSize(display.safe_size)
    ccui.Helper:doLayout(self.mUINode)
    self:addChild(self.mUINode)

    self.timeLabel = self.mUINode:findChild("Panel/num")
    assert(self.timeLabel)

    self.leftNameNode = self.mUINode:findChild("Panel/name_1")
    assert(self.leftNameNode)

    self.rightNameNode = self.mUINode:findChild("Panel/name_2")
    assert(self.rightNameNode)

    self.leftHpBar = self.mUINode:findChild("Panel/LoadingBar_1")
    assert(self.leftHpBar)

    self.rightHpBar = self.mUINode:findChild("Panel/LoadingBar_2")
    assert(self.rightHpBar)
    self._hpVisibleFlag = true

    self.leftHeadNodes = {}
    self.rightHeadNodes = {}
    for i=1,3 do
        --左方
        self.leftHeadNodes[i] = self.mUINode:findChild("Panel/blue_"..i)
        --右方
        self.rightHeadNodes[i] = self.mUINode:findChild("Panel/red_"..i)
    end


    self.selfIndex = nil
    self.enemyIndex = nil

    --设置事件
    self:init()
end

function KofTopView:init()
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_KOF_UI_HEAD_INIT,handler(self,KofTopView.initHeadData))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_KOF_UPDATE_DATA,handler(self,KofTopView.setData))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_KOF_HP_UPDATE,handler(self,KofTopView.updateHp))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_KOF_HP_HIDE,handler(self,KofTopView._shouldHideAllHpUI))

    self:setSetup()
end

function KofTopView:setSetup()
    --touchEvent
end

function KofTopView:setData(event)
    local leftKofRoundTime = event.leftKofRoundTime
    local selfTeamIndex = event.selfTeamIndex
    local enemyTeamIndex = event.enemyTeamIndex

    self:setTime(leftKofRoundTime)
    -- print("KofTopView:setData selfTeamIndex",selfTeamIndex)
    -- print("KofTopView:setData enemyTeamIndex",enemyTeamIndex)
    self:refreshHeadData(selfTeamIndex or 1, enemyTeamIndex or 1)
end

function KofTopView:setTime(time)
    time = tonumber(time)
    local second = math.floor(time/1000) -- 总秒
    self.timeLabel:setString(tostring(second))
    -- if time < 10000 then
    --     self.timeLabel:setColor(ColorWarnning)
    -- else
    --     self.timeLabel:setColor(ColorNormal)
    -- end
end

function KofTopView:updateHp(event)
    local uid = event.uid
    local hp = event.hp
    local hpMax = event.hpMax
    local hpBar = nil
    if uid == self.myUid then
        if self.isSelfLeft then
            hpBar = self.leftHpBar
        else
            hpBar = self.rightHpBar
        end
    elseif uid == self.enemyUid then
        if self.isSelfLeft then
            hpBar = self.rightHpBar
        else
            hpBar = self.leftHpBar
        end
    end

    if hpBar then
        local percent = hp*100/hpMax
        hpBar:setPercent(percent)
    end
end

function KofTopView:initHeadData(event)
    self.myKofUIData = event.myKofUIData
    self.enemyKofUIData = event.enemyKofUIData
    self.myUid = event.myUid
    self.enemyUid = event.enemyUid
    self.myTeamId = event.myTeamId
    self.enemyTeamId = event.enemyTeamId
    -- local leftKofUIData
    -- local rightKofUIData
    -- if self.isSelfLeft then
    --     leftKofUIData = self.myKofUIData
    --     rightKofUIData = self.enemyKofUIData
    -- else
    --     leftKofUIData = self.enemyKofUIData
    --     rightKofUIData = self.myKofUIData
    -- end
    -- for i=1,3 do
    --     --左方
    --     local leftNode = self.leftHeadNodes[i]
    --     if i > #leftKofUIData then
    --         --超过总数的要隐藏
    --         leftNode:setVisible(false)
    --     else
    --         leftNode:setVisible(true)
    --     end

    --     --右方
    --     local rightNode = self.rightHeadNodes[i]
    --     if i > #rightKofUIData then
    --         --超过总数的要隐藏
    --         rightNode:setVisible(false)
    --     else
    --         rightNode:setVisible(true)
    --     end
    -- end

    self:refreshHeadData(1, 1)
end

function KofTopView:refreshHeadData(selfIndex, enemyIndex)
    -- print("=======selfIndex",selfIndex)
    -- print("=======enemyIndex",enemyIndex)
    --检查是否有变化
    if self.selfIndex == selfIndex and self.enemyIndex == enemyIndex then
        return
    end

    self.selfIndex = selfIndex
    self.enemyIndex = enemyIndex

    local leftKofUIData
    local rightKofUIData
    local leftIndex
    local rightIndex
    if self.isSelfLeft then
        leftKofUIData = self.myKofUIData
        rightKofUIData = self.enemyKofUIData

        leftIndex = self.selfIndex
        rightIndex = self.enemyIndex
    else
        leftKofUIData = self.enemyKofUIData
        rightKofUIData = self.myKofUIData

        leftIndex = self.enemyIndex
        rightIndex = self.selfIndex
    end

    local leftCount = #leftKofUIData
    local rightCount = #rightKofUIData
    for i=1,3 do
        --左方
        local leftNode = self.leftHeadNodes[i]
        if i > leftCount then
            --超过总数的要隐藏
            leftNode:setVisible(false)
        else
            leftNode:setVisible(true)
        end

        --右方
        local rightNode = self.rightHeadNodes[i]
        if i > rightCount then
            --超过总数的要隐藏
            rightNode:setVisible(false)
        else
            rightNode:setVisible(true)
        end
    end

    --左侧头像
    --node 1 --对应 头像head index
    --node 2 --对应 头像head index + 1
    --node 3 --对应 头像head index + 2

    -- 如果head index + x 超过了leftCount
    -- for i=1, leftCount then
    --     local leftNode = self.leftHeadNodes[i]
    --     --获取数据表里的index
    --     local index = leftIndex + i - 1
    --     local isDead = false
    --     if index > leftCount then
    --         index = index - leftCount
    --         isDead = true
    --     end

    --     local data = leftKofUIData[index]
    --     --todo 刷新ui界面
    --     local iconFile = data.icon
    --     local iconNode = leftNode:findChild("head")
    --     if iconNode and iconFile then
    --         iconNode:loadTexture(iconFile)
    --     end

    --     local deadNode = leftNode:findChild("icon_die")
    --     if deadNode then
    --         deadNode:setVisible(isDead)
    --     end

    --     --第一个界面要刷新名字
    --     if i == 1 then
    --         self.leftNameNode:setString(data.name)
    --     end
    -- end
    self:refreshSideHead(leftCount, self.leftHeadNodes, leftIndex, leftKofUIData, self.leftNameNode)

    --todo right
    self:refreshSideHead(rightCount, self.rightHeadNodes, rightIndex, rightKofUIData, self.rightNameNode)
end

function KofTopView:refreshSideHead(count, nodeList, sideIndex, dataList, nameNode)
    --node 1 --对应 头像head index
    --node 2 --对应 头像head index + 1
    --node 3 --对应 头像head index + 2

    -- 如果head index + x count
    for i=1, count do
        local rootNode = nodeList[i]
        --获取数据表里的index
        local index = sideIndex + i - 1
        local isDead = false
        if index > count then
            index = index - count
            isDead = true
        end

        local data = dataList[index]
        --todo 刷新ui界面
        local iconFile = data.icon
        local iconNode = rootNode:findChild("head")
        --print("========KofTopView:refreshSideHead iconFile:",iconFile)
        if iconNode and iconFile then
            iconNode:loadTexture(iconFile)
            if isDead then
                iconNode:setOpacity(255*0.2)
            else
                iconNode:setOpacity(255)
            end

            -- iconNode:setEnabled(not isDead)
        end

        local deadNode = rootNode:findChild("icon_die")
        if deadNode then
            deadNode:setVisible(isDead)
        end

        --第一个界面要刷新名字
        if i == 1 then
            nameNode:setString(data.name)
        end
    end
end

-- 隐藏血条显示
function KofTopView:_shouldHideAllHpUI( b )
    if self._hpVisibleFlag == b then return end
    self._hpVisibleFlag = b

    -- 设置原血条可见性
    -- self.leftHpBar:setVisible( not b )

    -- 隐藏血条时特效的显示
    if cc.isDead( self.hpHideEffectNode ) then
        local params = {
            path = Res.HideHpEffect_1.dir,
            armatureName = Res.HideHpEffect_1.name,
            animationName = "",
            skeletonName = Res.HideHpEffect_1.name
        }
        local Armature = app:getClass("Armature")
        self.hpHideEffectNode = Armature.create( params )
        self.leftHpBar:getParent():addChild( self.hpHideEffectNode )
        self.hpHideEffectNode:setPosition( self.leftHpBar:getPosition() )
    end
    self.hpHideEffectNode:setVisible( b )
    if b then
        self.hpHideEffectNode:play( "effcreat", 1, function()
            self.hpHideEffectNode:play( "effloop", 1, function()
                self.hpHideEffectNode:play( "effdead", 1 )
            end )
        end )
    end
end

function KofTopView:clearHideEffect()
    if not cc.isDead(self.hpHideEffectNode) then
        self.hpHideEffectNode:removeFromParent()
        self.hpHideEffectNode = nil
    end
end

return KofTopView
